﻿using System;
using System.Drawing;
using WiMo.Games.Drawables;
using System.Drawing.Imaging;

namespace WiMo.Games.Premium.GDIDisplay.Drawables
{
    public class Rectangle : SpriteShapeBase, ISpriteShape
    {
        public Rectangle()
        {
            ShapeType = ShapeType.Rectangle;
            CollisionShape = CollisionShapes.Rectangle;
        }

        public override void Render(IDisplay display)
        {
            Render(display, new WiMo.Games.Rectangle(0, 0, display.Size.Width, display.Size.Height));            
        }

        public override bool CheckForCollision(ISprite sprite)
        {
            return false;
        }

        public override RenderStrategy RenderStrategy
        {
            get
            {
                return base.RenderStrategy;
            }
            set
            {
                if (DrawInformation != null)
                {
                    var image = DrawInformation as WiMoImage;
                    image.RenderStrategy = value;
                }

                base.RenderStrategy = value;
            }
        }

        private object _drawInformation;
        public override object DrawInformation
        {
            get
            {
                return _drawInformation;
            }
            set
            {
                _drawInformation = value;
            }
        }


        internal void Render(IDisplay display, WiMo.Games.Rectangle destRegion)
        {
            var displayManager = display as DisplayManager;

            var image = DrawInformation as WiMoImage;
            if (image == null)
            {
                image = WiMoImage.CreateForRect(this);
                DrawInformation = image;
            }

            var targetRect = BoundingRect.GetIntersection(destRegion);
            targetRect = targetRect + display.DisplayOffset;
            var srcRect = new WiMo.Games.Rectangle(Math.Max(0, destRegion.X - BoundingRect.X), Math.Max(0, destRegion.Y - BoundingRect.Y), targetRect.Width, targetRect.Height);

            image.RenderStrategy = RenderStrategy;

            var position = displayManager.GetAdjustedPosition(this);

            displayManager.Graphics.DrawImage(DrawInformation as WiMoImage, new Location(position.X, position.Y));
        }
    }
}
